--
-- Author: yawei.zhu
-- Date: 2018-06-23 18:10:50
--
local PvpChampionReady = class("PvpChampionReady")

function PvpChampionReady:ctor(node)
	self._readyNode = node
	self:initNode()
end

function PvpChampionReady:initNode()
	local readyPanel = self._readyNode:getChildByName("Panel")
	-- 交互界面
	readyPanel:addClickEventListener(function(sender)
		local touchPoint = sender:getTouchEndPosition()
		for i = 1, 3 do
			local character = self["_node"..i]:getChildByTag(0)
			if character and character:containWorldPoint(touchPoint.x, touchPoint.y) then
				local heroParams = self._heroParams[i]
				if heroParams.uid ~= UD:getUid() then
                	UIHandler:showHeadMenu({type=Enums.MenuType.champion, attr=heroParams, pos = touchPoint})
                end
				break
			end
		end
	end)

	local tlt = readyPanel:findChild("Panel_tlt/tlt")
	tlt:setString( L("champion_notice5") )

	self._overBg = readyPanel:getChildByName("bg_over")
	self._overPanel = readyPanel:getChildByName("Panel_over")
	self._txtRank = self._overPanel:getChildByName("Text_9")
	self._txtRank:setString("")

	self._readyPanel = readyPanel:getChildByName("Panel_ready")

	self._btnSwitch = readyPanel:getChildByName("btn_esc")
	self._btnSwitch:setVisible(false)
	self._btnSwitch:addClickEventListener(function()
		executeFunc(self._onOperate, "PvpChampion_CancelSign")
	end)

	self._titleTlt = readyPanel:getChildByName("tlt")

	local bomPanel = readyPanel:getChildByName("Panel_bom")
	local btnFound = bomPanel:getChildByName("Button_found")
	btnFound:setVisible(false)
	local buttonNode = bomPanel:getChildByName("Button_tips")
	buttonNode:getChildByName("Panel_1"):addClickEventListener(function()
		executeFunc(self._onOperate, "PvpChampion_search_3")
	end)
	self.btnEnter = bomPanel:getChildByName("Button_enter")
	self.btnName = self.btnEnter:getChildByName("name")
	self.btnEnter:addClickEventListener(function()
		print(self.btnName:getString())
		if self.btnName:getString() == L("champion_register3") then
			local confirmDialog = display.newConfirmDialog( L("champion_register4"), 
				function()
					executeFunc(self._onOperate, "PvpChampion_CancelSign")
				end)
			UIHandler:addPopDialog(confirmDialog)
		elseif self.btnName:getString() == L("champion_battle1") then
			executeFunc(self._onOperate, "PvpChampion_EnterMatch")
		end
	end)

	self._bomText = readyPanel:findChild("Panel_bom/Text")
	self._bomText:setString("")

	self._node1 = readyPanel:getChildByName("Node_1")
	self._node2 = readyPanel:getChildByName("Node_2")
	self._node3 = readyPanel:getChildByName("Node_3")

	self._data1 = readyPanel:getChildByName("data_1")
	self._data2 = readyPanel:getChildByName("data_2")
	self._data3 = readyPanel:getChildByName("data_3")
end

function PvpChampionReady:tick(throneGame)
	local remainedTime = throneGame.battleStartTime - app.session:getServerTime()
	if remainedTime <= 0 then 
		self._bomText:setString( L("champion_battle2") )
		return
	end
	local hour = math.floor(remainedTime / 3600)
	local min = math.floor((remainedTime - hour * 3600) / 60 )
	local sec = math.floor(remainedTime % 60)
	self._bomText:setString( string.format( L("champion_battle3"), hour, min, sec ) )

	-- 报名阶段 -> 报名截止阶段 的时候, 按钮名字需要转换
	if self.btnName:getString() == L("champion_register3") and throneGame:checkChampionStatus() == Enums.ChampionStatus.PREPARE then
		self.btnName:setString( L("champion_battle1") )
	end
end

-- 显示队伍成员
function PvpChampionReady:updateCharacter(throneTeam)
	for i = 1, 3 do
		self["_node"..i]:removeAllChildren()
    	self["_data"..i]:setVisible(false)
	end
	self._heroParams = {}
	if not throneTeam then return end

	for i = 1, 3 do
		local heroParams = throneTeam:getUserBaseInfo(i)
		if heroParams then
			self._heroParams[i] = heroParams 
			heroParams.inBattle = false
    		heroParams.mountID = 0
	
    		local AvatarFactory = app:getClass("AvatarFactory")
    		AvatarFactory.preloadHero(heroParams, function()
    		    if tolua.isnull(self._readyNode) then return end
	
    		    local character = AvatarFactory.createHero(heroParams)
    		    self["_node"..i]:addChild(character)
    		    character:playAni({name = "mapidle"})
    		    character:setTag(0)
    		    character:setScale(0.9 * 0.85)
    		    -- 自动调整名字的高度
    		    character:addAttachment(self["_data"..i], "point", 0, 0)
    		end)

			local node = self["_data"..i]
    		local name = node:getChildByName("Text_name")
    		local icon = node:getChildByName("icon")
    		local txtPow = node:getChildByName("num")
	
			node:setVisible(true)
    		name:setString(heroParams.name)
    		txtPow:setString(heroParams.combatPower)
			icon:loadTexture( heroParams:getClassIcon() )
	
			-- 调整 icon 和 战力 的坐标
			display.setNodesMiddle(node, nil, icon, txtPow)
		end
	end
end

function PvpChampionReady:showRank(visible, rankInfo)
	self._overBg:setVisible(visible)
	self._overPanel:setVisible(visible)
	-- self._btnSwitch:setVisible(not visible)	

	if visible then
		self.btnName:setString( L("champion_battle1") )
		self._titleTlt:setString( L("champion_battle4") )

		if app:sendMsg("NewActivityDungeonsModel", "isChampionMap") then
			self.btnEnter:setVisible(false)
		else
			self.btnEnter:setVisible(true)
		end
		self:updateChampionRank(rankInfo)
	else
		self.btnName:setString( L("champion_register3") )
		self._titleTlt:setString( L("champion_battle5") )
	end
end

function PvpChampionReady:updateChampionRank(rankInfo)

	if self._richText then 
		self._richText:removeFromParent()
		self.richText = nil
	end

	local defaults = {
        KEY_FONT_FACE = Res.font,
        KEY_FONT_SIZE = 24,
        KEY_FONT_COLOR_STRING = "#FFFFFF",
    }
    local str1 = display.getRichTextFontXml( string.format("%d", rankInfo.wonTimes), "#75d37c", 28)
    local str2 = display.getRichTextFontXml( string.format("%d", rankInfo.lostTimes), "#FF0000", 28)
    local str3 = display.getRichTextFontXml( string.format("%d", rankInfo.rank), "#EEE9BF", 28)
   	local str = string.format(L("champion_battle6"), str1, str2, str3)

    local richText = ccui.RichText:createWithXML( str, defaults )
    richText:ignoreContentAdaptWithSize( true )
    -- richText:setWrapMode( 1 )
    -- richText:setContentSize( cc.size( 500, 0 ) )
    richText:formatText()
    richText:setPosition( self._overPanel:getContentSize().width / 2, self._overPanel:getContentSize().height / 2 )
    richText:addTo(self._overPanel)

    self._richText = richText
end

function PvpChampionReady:setShow(visible)
	self._readyNode:setVisible(visible)
end

function PvpChampionReady:isVisible()
	return self._readyNode:isVisible()
end

function PvpChampionReady:onOperate(cb)
	self._onOperate = cb
end


return PvpChampionReady

